clear; clc; close all;

% 绘制的迭代步数 (根据需要可自行调节)
N = 100;


%% 1) 2D-MLM
mu_mlm = 0.2; 
k_mlm  = 2.08;
x0_mlm = 0.2; 
q0_mlm = 0.1;
xMLM = sequence(@MLM, mu_mlm, k_mlm, x0_mlm, q0_mlm, N);


%% 2) 2D-MSM
mu_msm = -0.1; 
k_msm  = 1.88;
x0_msm = 0.8; 
q0_msm = 0.6;
xMSM = sequence(@MSM, mu_msm, k_msm, x0_msm, q0_msm, N);


%% 3) 2D-MTM
mu_mtm = -0.6; 
k_mtm  = 1.78;
x0_mtm = 0.8; 
q0_mtm = -0.8;
xMTM = sequence(@MTM, mu_mtm, k_mtm, x0_mtm, q0_mtm, N);


%% 4) 2D-SMM
mu_smm = 0.5; 
k_smm  = 2.33;
x0_smm = 1; 
q0_smm = 1;
xSMM = sequence(@SMM, mu_smm, k_smm, x0_smm, q0_smm, N);


%% 绘图
% (a) 2D-MLM
subplot(4,1,1);
plot(1:N, xMLM, 'm', 'LineWidth',1.5);
ylabel('x'); grid on;
title('2D-MLM (\mu=0.2, k=2.08)');

% (b) 2D-MSM
subplot(4,1,2);
plot(1:N, xMSM, 'Color',[0 0.6 0], 'LineWidth',1.5); % 类似绿色
ylabel('x'); grid on;
title('2D-MSM (\mu=-0.1, k=1.88)');

% (c) 2D-MTM
subplot(4,1,3);
plot(1:N, xMTM, 'Color',[1 0.5 0], 'LineWidth',1.5); % 橙色
ylabel('x'); grid on;
title('2D-MTM (\mu=-0.6, k=1.78)');

% (d) 2D-SMM
subplot(4,1,4);
plot(1:N, xSMM, 'k', 'LineWidth',1.5);
xlabel('n'); ylabel('x'); grid on;
title('2D-SMM (\mu=0.5, k=2.33)');


%% 辅助函数：返回 x 序列
function xdata = sequence(map_func, mu, k, x0, q0, N)
    x = x0;
    q = q0;
    xdata = zeros(1, N);
    xdata(1) = x0;
    
    for i = 2:N
        [x, q] = map_func(x, q, mu, k);
        xdata(i) = x;
    end
end


%% 四种迭代函数
function [x_next, q_next] = MLM(x, q, mu, k)
    % 2D-MLM:
    % x_{n+1} = mu*x_n*(1 - x_n) + k*cos(q_n)*x_n
    % q_{n+1} = q_n + x_n
    x_next = mu*x*(1 - x) + k*cos(q)*x;
    q_next = q + x;
end

function [x_next, q_next] = MSM(x, q, mu, k)
    % 2D-MSM:
    % x_{n+1} = mu*sin(2*pi*x_n) + k*cos(q_n)*x_n
    % q_{n+1} = q_n + x_n
    x_next = mu*sin(2*pi*x) + k*cos(q)*x;
    q_next = q + x;
end

function [x_next, q_next] = MTM(x, q, mu, k)
    % 2D-MTM (分段):
    % 若 x < 0.5:
    %   x_{n+1} = mu*x + k*cos(q)*x
    % 否则:
    %   x_{n+1} = mu*(1 - x) + k*cos(q)*x
    % q_{n+1} = q_n + x_n
    if x < 0.5
        x_next = mu*x + k*cos(q)*x;
    else
        x_next = mu*(1 - x) + k*cos(q)*x;
    end
    q_next = q + x;
end

function [x_next, q_next] = SMM(x, q, mu, k)
    % 2D-SMM:
    % x_{n+1} = mu*x + k*cos(q)*x
    % q_{n+1} = q_n + x_n
    x_next = mu*x + k*cos(q)*x;
    q_next = q + x;
end
